package com.bkgy.modular.tbpltcmascresult.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.tbpltcmascresult.dao.TbPltcmAscResultMapper;
import com.bkgy.modular.tbpltcmascresult.entity.ExporttbPltcmAscResult;
import com.bkgy.modular.tbpltcmascresult.entity.TbPltcmAscResult;
import com.bkgy.modular.tbpltcmascresult.entity.TbPltcmAscResultCollect;
import com.bkgy.modular.tbpltcmascresult.entity.TbPltcmAscResultLine;
import com.bkgy.modular.tbpltcmascresult.service.TbPltcmAscResultService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CalUtils;
import com.bkgy.util.ExcelUtils;
import com.bkgy.util.ExcelUtilsPOI;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class TbPltcmAscResultServiceImpl extends ServiceImpl<TbPltcmAscResultMapper, TbPltcmAscResult>
    implements TbPltcmAscResultService {

    @Resource
    private CalUtils calUtils;

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private TbPltcmAscResultMapper tbPltcmAscResultMapper;

    @Override
    public ResultData findDataByPage(JSONObject json) {
        try {
            Integer pageIndex = json.getInteger("pageIndex");
            Integer pageSize = json.getInteger("pageSize");
            //查询条件暂无，后续需要再根据需求添加
            JSONObject condition = json.getJSONObject("condition");
            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }
            String dbegin = null;//开始时间
            String dend = null;//结束时间
            String extid = null;//出口卷号
            String entid = null;//入口卷号
            String steelGrade = null;//钢种
            Integer pltcm_no = null;//产线
            BigDecimal exThickBegin = null;//产品厚度开始
            BigDecimal exThickEnd = null;//产品厚度结束
            BigDecimal enThickBegin = null;//原料厚度开始
            BigDecimal enThickEnd = null;//原料厚度结束
            BigDecimal exWidthBegin = null;//产品宽度开始
            BigDecimal exWidthEnd = null;//产品宽度结束
            BigDecimal enWidthBegin = null;//原料宽度开始
            BigDecimal enWidthEnd = null;//原料宽度结束

            QueryWrapper<TbPltcmAscResult> wrapper = new QueryWrapper<>();
            if (!condition.isEmpty()) {
                //开始时间
                if (StringUtils.hasText(condition.getString("dbegin"))) {
                    dbegin = condition.getString("dbegin");
                    String time = calUtils.calDateTime(dbegin);
                    wrapper.lambda().gt(TbPltcmAscResult::getEnd_time,time);
                }
                //结束时间
                if (StringUtils.hasText(condition.getString("dend"))) {
                    dend = condition.getString("dend");
                    String time = calUtils.calDateTime(dend);
                    wrapper.lambda().lt(TbPltcmAscResult::getEnd_time,time).orderByDesc(TbPltcmAscResult::getEnd_time);
                }
                //入口卷号
                if (StringUtils.hasText(condition.getString("entid"))) {
                    entid = condition.getString("entid");
                    wrapper.lambda().eq(TbPltcmAscResult::getEntid,entid);
                }
                //出口卷号
                if (StringUtils.hasText(condition.getString("extid"))) {
                    extid = condition.getString("extid");
                    wrapper.lambda().eq(TbPltcmAscResult::getExtid,extid);
                }
                //钢种
                if (StringUtils.hasText(condition.getString("steelGrade"))) {
                    steelGrade = condition.getString("steelGrade");
                    wrapper.lambda().eq(TbPltcmAscResult::getSteel_grade,steelGrade);
                }
                //产线
                if (!ObjectUtils.isEmpty(condition.getInteger("pltcm_no"))) {
                    pltcm_no = condition.getInteger("pltcm_no");
                    wrapper.lambda().eq(TbPltcmAscResult::getPltcm_no,pltcm_no);
                }
                //产品厚度
                if (!ObjectUtils.isEmpty(condition.getBigDecimal("exThickBegin"))) {
                    exThickBegin = condition.getBigDecimal("exThickBegin");
                    wrapper.lambda().gt(TbPltcmAscResult::getThickness,exThickBegin);
                }
                if (!ObjectUtils.isEmpty(condition.getBigDecimal("exThickEnd"))) {
                    exThickEnd = condition.getBigDecimal("exThickEnd");
                    wrapper.lambda().lt(TbPltcmAscResult::getThickness,exThickEnd);
                }
                if (!ObjectUtils.isEmpty(condition.getBigDecimal("enThickBegin"))) {
                    enThickBegin = condition.getBigDecimal("enThickBegin");
                    wrapper.lambda().gt(TbPltcmAscResult::getEn_thickness,enThickBegin);
                }
                if (!ObjectUtils.isEmpty(condition.getBigDecimal("enThickEnd"))) {
                    enThickEnd = condition.getBigDecimal("enThickEnd");
                    wrapper.lambda().lt(TbPltcmAscResult::getEn_thickness,enThickEnd);
                }
                //产品宽度
                if (!ObjectUtils.isEmpty(condition.getBigDecimal("exWidthBegin"))) {
                    exWidthBegin = condition.getBigDecimal("exWidthBegin");
                    wrapper.lambda().gt(TbPltcmAscResult::getWidth,exWidthBegin);
                }
                if (!ObjectUtils.isEmpty(condition.getBigDecimal("exWidthEnd"))) {
                    exWidthEnd = condition.getBigDecimal("exWidthEnd");
                    wrapper.lambda().lt(TbPltcmAscResult::getWidth,exWidthEnd);
                }
                if (!ObjectUtils.isEmpty(condition.getBigDecimal("enWidthBegin"))) {
                    enWidthBegin = condition.getBigDecimal("enWidthBegin");
                    wrapper.lambda().gt(TbPltcmAscResult::getEn_width,enWidthBegin);
                }
                if (!ObjectUtils.isEmpty(condition.getBigDecimal("enWidthEnd"))) {
                    enWidthEnd = condition.getBigDecimal("enWidthEnd");
                    wrapper.lambda().lt(TbPltcmAscResult::getEn_width,enWidthEnd);
                }
            }
            /*Page<TbPltcmAscResult> page = new Page<>(pageIndex, pageSize);
            Page<TbPltcmAscResult> result = this.page(page, wrapper);
            List<TbPltcmAscResult> resultNoPage = this.list(wrapper);*/
            PageHelper.startPage(pageIndex,pageSize); // 每页的大小为pageSize,查询第page也的结果
            List<TbPltcmAscResult> resultNoPage = tbPltcmAscResultMapper.selectList(wrapper);
            Integer sum1 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getClass1())).mapToInt(TbPltcmAscResult::getClass1).sum();
            Integer sum2 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getClass2())).mapToInt(TbPltcmAscResult::getClass2).sum();
            Integer sum3 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getClass3())).mapToInt(TbPltcmAscResult::getClass3).sum();
            Integer sum4 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getClass4())).mapToInt(TbPltcmAscResult::getClass4).sum();
            Integer sum5 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getClass5())).mapToInt(TbPltcmAscResult::getClass5).sum();
            Integer sum6 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getClass6())).mapToInt(TbPltcmAscResult::getClass6).sum();
            Integer sum7 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getClass7())).mapToInt(TbPltcmAscResult::getClass7).sum();

            TbPltcmAscResultCollect collect = new TbPltcmAscResultCollect();
            collect.setSum1(sum1);
            collect.setSum2(sum2);
            collect.setSum3(sum3);
            collect.setSum4(sum4);
            collect.setSum5(sum5);
            collect.setSum6(sum6);
            collect.setSum7(sum7);

            PageInfo<TbPltcmAscResult> pageInfo = new PageInfo<>(resultNoPage);
            PageHelper.clearPage();
            return ResultData.ResultDataSuccess(resultNoPage, (int) pageInfo.getTotal(),collect);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData findLineDataByVolumeNo(JSONObject json) {
        String volumeNo = json.getString("volumeNo");
        if (!StringUtils.hasText(volumeNo)) {
            return ResultData.ResultDataFaultSelf(null, "传入卷号为空");
        }
        Query query1 = new Query(Criteria.where("extid").is(volumeNo));
        TbPltcmAscResultLine tbPltcmAscResultLine = mongoTemplate.findOne(query1, TbPltcmAscResultLine.class);
        if (!ObjectUtils.isEmpty(tbPltcmAscResultLine)) {
            return ResultData.ResultDataSuccess(tbPltcmAscResultLine);
        }
        return ResultData.ResultDataFaultSelf(null, "查询数据为空");
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        String dbegin = null;//开始时间
        String dend = null;//结束时间
        String extid = null;//出口卷号
        String entid = null;//入口卷号
        String steelGrade = null;//钢种
        Integer pltcm_no = null;//产线
        BigDecimal exThickBegin = null;//产品厚度开始
        BigDecimal exThickEnd = null;//产品厚度结束
        BigDecimal enThickBegin = null;//原料厚度开始
        BigDecimal enThickEnd = null;//原料厚度结束
        BigDecimal exWidthBegin = null;//产品宽度开始
        BigDecimal exWidthEnd = null;//产品宽度结束
        BigDecimal enWidthBegin = null;//原料宽度开始
        BigDecimal enWidthEnd = null;//原料宽度结束

        QueryWrapper<TbPltcmAscResult> wrapper = new QueryWrapper<>();
        if (!condition.isEmpty()) {
            //开始时间
            if (StringUtils.hasText(condition.getString("dbegin"))) {
                dbegin = condition.getString("dbegin");
                String time = calUtils.calDateTime(dbegin);
                wrapper.lambda().gt(TbPltcmAscResult::getEnd_time,time);
            }
            //结束时间
            if (StringUtils.hasText(condition.getString("dend"))) {
                dend = condition.getString("dend");
                String time = calUtils.calDateTime(dend);
                wrapper.lambda().lt(TbPltcmAscResult::getEnd_time,time).orderByDesc(TbPltcmAscResult::getEnd_time);
            }
            //入口卷号
            if (StringUtils.hasText(condition.getString("entid"))) {
                entid = condition.getString("entid");
                wrapper.lambda().eq(TbPltcmAscResult::getEntid,entid);
            }
            //出口卷号
            if (StringUtils.hasText(condition.getString("extid"))) {
                extid = condition.getString("extid");
                wrapper.lambda().eq(TbPltcmAscResult::getExtid,extid);
            }
            //钢种
            if (StringUtils.hasText(condition.getString("steelGrade"))) {
                steelGrade = condition.getString("steelGrade");
                wrapper.lambda().eq(TbPltcmAscResult::getSteel_grade,steelGrade);
            }
            //产线
            if (!ObjectUtils.isEmpty(condition.getInteger("pltcm_no"))) {
                pltcm_no = condition.getInteger("pltcm_no");
                wrapper.lambda().eq(TbPltcmAscResult::getPltcm_no,pltcm_no);
            }
            //产品厚度
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("exThickBegin"))) {
                exThickBegin = condition.getBigDecimal("exThickBegin");
                wrapper.lambda().gt(TbPltcmAscResult::getThickness,exThickBegin);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("exThickEnd"))) {
                exThickEnd = condition.getBigDecimal("exThickEnd");
                wrapper.lambda().lt(TbPltcmAscResult::getThickness,exThickEnd);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("enThickBegin"))) {
                enThickBegin = condition.getBigDecimal("enThickBegin");
                wrapper.lambda().gt(TbPltcmAscResult::getEn_thickness,enThickBegin);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("enThickEnd"))) {
                enThickEnd = condition.getBigDecimal("enThickEnd");
                wrapper.lambda().lt(TbPltcmAscResult::getEn_thickness,enThickEnd);
            }
            //产品宽度
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("exWidthBegin"))) {
                exWidthBegin = condition.getBigDecimal("exWidthBegin");
                wrapper.lambda().gt(TbPltcmAscResult::getWidth,exWidthBegin);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("exWidthEnd"))) {
                exWidthEnd = condition.getBigDecimal("exWidthEnd");
                wrapper.lambda().lt(TbPltcmAscResult::getWidth,exWidthEnd);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("enWidthBegin"))) {
                enWidthBegin = condition.getBigDecimal("enWidthBegin");
                wrapper.lambda().gt(TbPltcmAscResult::getEn_width,enWidthBegin);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("enWidthEnd"))) {
                enWidthEnd = condition.getBigDecimal("enWidthEnd");
                wrapper.lambda().lt(TbPltcmAscResult::getEn_width,enWidthEnd);
            }
            List<TbPltcmAscResult> listExcel = tbPltcmAscResultMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(listExcel)) {
                return;
            }
            //log.error("TbPltcmAscResultServiceImpl数据查询完成: " );
            List<ExporttbPltcmAscResult> list = new ArrayList<>();
            if (!listExcel.isEmpty() && listExcel.size() > 0) {
                for (TbPltcmAscResult item : listExcel) {
                    ExporttbPltcmAscResult exporttbPltcmAscResult = new ExporttbPltcmAscResult();
                    BeanUtils.copyProperties(item,exporttbPltcmAscResult);
                    list.add(exporttbPltcmAscResult);
                }
            }
            String path="";
            //log.error("TbPltcmAscResultServiceImpl数据转换完成: " );
            if(pltcm_no==2130)
            {
                path="classpath:exceltemplate\\2130酸轧板形精度.xlsx";
            }
            if(pltcm_no==1720)
            {
                path="classpath:exceltemplate\\1720板形精度.xlsx";
            }
            try {
                Map<String,Object> data = new HashMap();
                data.put("data",list);
                File file = ResourceUtils.getFile(path);
                TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
                templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
                Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data);
                ExcelUtilsPOI.exportExcel("1720板形精度",response,workbook);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //ExcelUtils.downloadExcel(fileNames,response, TbPltcmAscResult.class,listExcel);
        }
    }
    @Override
    public void exportold(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        String dbegin = null;//开始时间
        String dend = null;//结束时间
        String extid = null;//出口卷号
        String entid = null;//入口卷号
        String steelGrade = null;//钢种
        Integer pltcm_no = null;//产线
        BigDecimal exThickBegin = null;//产品厚度开始
        BigDecimal exThickEnd = null;//产品厚度结束
        BigDecimal enThickBegin = null;//原料厚度开始
        BigDecimal enThickEnd = null;//原料厚度结束
        BigDecimal exWidthBegin = null;//产品宽度开始
        BigDecimal exWidthEnd = null;//产品宽度结束
        BigDecimal enWidthBegin = null;//原料宽度开始
        BigDecimal enWidthEnd = null;//原料宽度结束

        QueryWrapper<TbPltcmAscResult> wrapper = new QueryWrapper<>();
        if (!condition.isEmpty()) {
            //开始时间
            if (StringUtils.hasText(condition.getString("dbegin"))) {
                dbegin = condition.getString("dbegin");
                String time = calUtils.calDateTime(dbegin);
                wrapper.lambda().gt(TbPltcmAscResult::getEnd_time,time);
            }
            //结束时间
            if (StringUtils.hasText(condition.getString("dend"))) {
                dend = condition.getString("dend");
                String time = calUtils.calDateTime(dend);
                wrapper.lambda().lt(TbPltcmAscResult::getEnd_time,time).orderByDesc(TbPltcmAscResult::getEnd_time);
            }
            //入口卷号
            if (StringUtils.hasText(condition.getString("entid"))) {
                entid = condition.getString("entid");
                wrapper.lambda().eq(TbPltcmAscResult::getEntid,entid);
            }
            //出口卷号
            if (StringUtils.hasText(condition.getString("extid"))) {
                extid = condition.getString("extid");
                wrapper.lambda().eq(TbPltcmAscResult::getExtid,extid);
            }
            //钢种
            if (StringUtils.hasText(condition.getString("steelGrade"))) {
                steelGrade = condition.getString("steelGrade");
                wrapper.lambda().eq(TbPltcmAscResult::getSteel_grade,steelGrade);
            }
            //产线
            if (!ObjectUtils.isEmpty(condition.getInteger("pltcm_no"))) {
                pltcm_no = condition.getInteger("pltcm_no");
                wrapper.lambda().eq(TbPltcmAscResult::getPltcm_no,pltcm_no);
            }
            //产品厚度
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("exThickBegin"))) {
                exThickBegin = condition.getBigDecimal("exThickBegin");
                wrapper.lambda().gt(TbPltcmAscResult::getThickness,exThickBegin);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("exThickEnd"))) {
                exThickEnd = condition.getBigDecimal("exThickEnd");
                wrapper.lambda().lt(TbPltcmAscResult::getThickness,exThickEnd);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("enThickBegin"))) {
                enThickBegin = condition.getBigDecimal("enThickBegin");
                wrapper.lambda().gt(TbPltcmAscResult::getEn_thickness,enThickBegin);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("enThickEnd"))) {
                enThickEnd = condition.getBigDecimal("enThickEnd");
                wrapper.lambda().lt(TbPltcmAscResult::getEn_thickness,enThickEnd);
            }
            //产品宽度
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("exWidthBegin"))) {
                exWidthBegin = condition.getBigDecimal("exWidthBegin");
                wrapper.lambda().gt(TbPltcmAscResult::getWidth,exWidthBegin);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("exWidthEnd"))) {
                exWidthEnd = condition.getBigDecimal("exWidthEnd");
                wrapper.lambda().lt(TbPltcmAscResult::getWidth,exWidthEnd);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("enWidthBegin"))) {
                enWidthBegin = condition.getBigDecimal("enWidthBegin");
                wrapper.lambda().gt(TbPltcmAscResult::getEn_width,enWidthBegin);
            }
            if (!ObjectUtils.isEmpty(condition.getBigDecimal("enWidthEnd"))) {
                enWidthEnd = condition.getBigDecimal("enWidthEnd");
                wrapper.lambda().lt(TbPltcmAscResult::getEn_width,enWidthEnd);
            }
            List<TbPltcmAscResult> listExcel = tbPltcmAscResultMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(listExcel)) {
                return;
            }
            ExcelUtils.downloadExcel(fileNames,response, TbPltcmAscResult.class,listExcel);
        }
    }
}




